package per.jerry.flightinformation.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import per.jerry.flightinformation.entity.FlightTickets;
import per.jerry.flightinformation.entity.Flights;
import per.jerry.flightinformation.service.FlightService;
@Controller
public class FlightTicketController {
	@Autowired
	private FlightService flightService;
	
	@RequestMapping(value="/flightTicket.html",method=RequestMethod.GET)
	public ModelAndView goToFlightTicket(){
		ModelAndView mv = new ModelAndView();
		//获取所有航班信息
		List<Flights> flights = flightService.findFlightsByParams(null, null, null);
		mv.addObject("flights", flights);
		mv.addObject("ft", new FlightTickets());
		mv.setViewName("flightticket");
		return mv;
	}
	
	@RequestMapping(value="/flightTicket.html",method=RequestMethod.POST)
	public String save(@Valid FlightTickets ft,BindingResult bindingResult){
		if(bindingResult.hasErrors()){
			return "flightticket";
		}
		//获取所有航班信息
		flightService.saveFlightTicket(ft);
		return "redirect:/flightList.html";
	}
	

	public FlightService getFlightService() {
		return flightService;
	}

	public void setFlightService(FlightService flightService) {
		this.flightService = flightService;
	}
}
